﻿using Cherry.Agile.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Security;

namespace Cherry.Agile.UI.Framework.Security
{
    /// <summary>
    /// 权限验证管理
    /// </summary>
    public static class AuthorizationManager
    {
        const string AUTHCOOKIE_PATH = "__agile";
        const string CURRENT_USER = "__current_user";
        /// <summary>
        /// 当前上下问绑定的用户信息
        /// </summary>
        public static User CurrentUser
        {
            get
            {
                if (Authorization.User == null || !(Authorization.User as IIdentity).IsAuthenticated)
                    return new User();
                return Authorization.User;
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>是否登录成功</returns>
        public static bool Login(Agile.Security.ITicket ticket, int userType)
        {
            return Login(ticket, false);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="createPersistent">是否保存用户信息</param>
        /// <returns>是否登录成功</returns>
        public static bool Login(Agile.Security.ITicket ticket, bool createPersistent)
        {
            var user = Authorization.Authorize(ticket, createPersistent);
            var identity = user as IIdentity;
            return (identity.IsAuthenticated);

        }

        /// <summary>
        /// 注销
        /// </summary>
        public static void SignOut()
        {
            Authorization.SignOut();
            //FormsAuthentication.SignOut();
            HttpContext.Current.Session.Abandon();
            FormsAuthentication.RedirectToLoginPage();

        }

        /// <summary>
        /// 验证用户信息
        /// </summary>
        internal static void Validate()
        {

            //if (url.IndexOf(".as", StringComparison.InvariantCultureIgnoreCase) < 0)
            //    return;
            if (HttpContext.Current.Request.IsAuthenticated)
            {
                string url = HttpContext.Current.Request.RawUrl;
                if (url.StartsWith(FormsAuthentication.LoginUrl, StringComparison.OrdinalIgnoreCase)
                    || !HttpContext.Current.Request.CurrentExecutionFilePathExtension.StartsWith(".as", StringComparison.InvariantCultureIgnoreCase))
                    return;

                var user = Authorization.User;
                if (user == null || !(user as IIdentity).IsAuthenticated)
                {
                    SignOut();
                    return;
                }

                //HttpContext.Current.User = System.Threading.Thread.CurrentPrincipal = user;

            }
        }


        #region private


        #endregion
    }
}
